package com.aurora.color;

import static android.graphics.Color.parseColor;
import static com.aurora.color.Utils.*;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.aurora.color.adapter.FuListAdapter;
import com.aurora.color.bean.Color;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class HomePage extends AppCompatActivity {

    private ListView listView;
    //测试第二次提交
    //测试第三次提交
    //测试第四次提交
    //测试第五次提交

    //颜色列表
    private List<Color> colorList;

    //幸运色
    private String[] lkColor;

    private final String TAG = "aurora";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        initDate();
        initView();

    }


    /**
     * 初始化列表
     */
    private void initView() {

        //设置幸运色
        RelativeLayout bar = findViewById(R.id.home_bar);
        bar.setBackgroundColor(parseColor(lkColor[1]));

        //切换幸运色按钮
        CardView lkCard = findViewById(R.id.card_btn);
        lkCard.setCardBackgroundColor(parseColor(lkColor[1]));
        lkCard.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setClass(this, HomePage.class);
            startActivity(intent);
            finish();
        });
        setBarColor(this, parseColor(lkColor[1]));
        sToast(this, "幸运色：" + lkColor[0]);


        //颜色列表
        listView = findViewById(R.id.color_list);
        FuListAdapter adapter = new FuListAdapter(colorList, this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            String[] fColor = colorList.get(i).getFColor();
            String[] zColor = colorList.get(i).getZColor();

            //跳转到子颜色列表
            Intent intent = new Intent();
            intent.setClass(HomePage.this, ColorDetail.class);
            intent.putExtra("fColor", fColor);
            intent.putExtra("zColor", zColor);
            startActivity(intent);

        });


    }

    /**
     * 初始化数据
     */
    private void initDate() {

        //颜色数据
        String colorData = getTxtContent(this, R.raw.colorcode);
        //幸运色
        String lkColorData = getTxtContent(this, R.raw.colorlucky);

        String[] colorDataArr = colorData.split("\n");
        String[] lkColorDataArr = lkColorData.split("\n");

        //随机选取一个颜色
        Random r = new Random();
        lkColor = lkColorDataArr[r.nextInt(lkColorDataArr.length)].
                split("\\|");

        //创建对象
        colorList = new ArrayList<>();

        for (
                String s : colorDataArr) {
            String fu = s.substring(0, s.indexOf("[")),
                    zi = s.substring(s.indexOf("[") + 1, s.length() - 1);
            String[] fColor = fu.split("\\|"),
                    zColor = zi.split("\\|");
            Color color = new Color(fColor, zColor);

            colorList.add(color);
        }


    }


}